﻿namespace Leetcode.N0429;

public class Solution
{
    public IList<IList<int>> LevelOrder(Node root)
    {

        var result = new List<IList<int>>();
        if (root == null)
        {
            return result;
        }

        var queue = new Queue<Node>();
        queue.Enqueue(root);

        while (queue.Count > 0)
        {
            var levelVals = new List<int>();
            result.Add(levelVals);

            var nodes = new List<Node>();
            while (queue.Count > 0)
            {
                var dNode = queue.Dequeue();
                levelVals.Add(dNode.val);
                nodes.Add(dNode);
            }

            foreach (var node in nodes)
            {
                if (node.children != null)
                {
                    foreach (var cNode in node.children)
                    {
                        queue.Enqueue(cNode);
                    }
                }
            }
        }
        return result;
    }
}
